//
//  EaseMobManage.m
//  ArtCircle
//
//  Created by sunbin on 2017/5/21.
//  Copyright © 2017年 HuaYue. All rights reserved.
//

#import "EaseMobManage.h"

@implementation EaseMobManage
single_implementation(EaseMobManage);


- (void)optionsWithAppkey:(NSString *)AppKey apnsCertName:(NSString *)apnsCertName {
    
    EMOptions *options = [EMOptions optionsWithAppkey:AppKey];
    options.apnsCertName = apnsCertName;
    [[EMClient sharedClient] initializeSDKWithOptions:options];
    
    
}


- (void)registerWithUsername:(NSString *)userName password:(NSString *)pwd success:(void (^)(NSString* successMsg))success failed:(void(^)(NSString *msg))failed {
    EMError *error = [[EMClient sharedClient] registerWithUsername:userName password:pwd];
    
    error==nil?success(@"注册成功"):failed(@"注册失败");
    
}


- (void)loginWithUsername:(NSString *)userName password:(NSString *)pwd success:(void (^)(NSString* successMsg))success failed:(void(^)(NSString *msg))failed{
    
    // 在您调用登录方法前，应该先判断是否设置了自动登录，如果设置了，则不需要您再调用。
    
    BOOL isAutoLogin = [EMClient sharedClient].options.isAutoLogin;
    
    if (!isAutoLogin) {
        EMError *error = [[EMClient sharedClient] loginWithUsername:userName password:pwd];
        if (!error) {
            NSLog(@"登录成功");
            
            // 自动登录
            // 自动登录：即首次登录成功后，不需要再次调用登录方法，在下次 APP 启动时，SDK 会自动为您登录。并且如果您自动登录失败，也可以读取到之前的会话信息。
            
            // SDK 中自动登录属性默认是关闭的，需要您在登录成功后设置，以便您在下次 APP 启动时不需要再次调用环信登录，并且能在没有网的情况下得到会话列表。
            
            [[EMClient sharedClient].options setIsAutoLogin:YES];
            
            success(@"环信登录成功");
        }else{
            failed(@"环信登录失败");
        }
    }else{
        success(@"环信已经登录");
    }
    
}

- (void)logout{
    EMError *error = [[EMClient sharedClient] logout:YES];
    if (!error) {
        NSLog(@"退出成功");
    }
    
}


@end
